<?php
namespace app\plus\controller;

use think\Controller;
use think\Db;

class AttachmentController extends Controller
{
    //获取分组
    private function getGroupList()
    {
        return $groupList = Db::table('attachment_group')->select();
    }

    //图片管理
    public function image()
    {
        $this->assign('groupList', $this->getGroupList());
        return $this->fetch();
    }

    //附件管理
    public function file()
    {
        $this->assign('groupList', $this->getGroupList());
        return $this->fetch();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $page       = $this->request->param('page/d', 0);
            $utype      = $this->request->param('utype');
            $selectType = $this->request->param('selectType');
            $groupId    = $this->request->param('groupId/d', 0);
            switch ($selectType) {
                case 'image':
                    $suffix = [".png", ".jpg", ".jpeg", ".gif", ".bmp"];
                    break;
                case 'file':
                    $suffix = [
                        ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".pem",
                        ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml",
                    ];
                    break;
                case 'video':
                    $suffix = [
                        ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                        ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
                    ];
                    break;
            }
            $pageSize          = 18;
            $limit             = (($page - 1) * 18) . ',' . $pageSize;
            $map               = [];
            $map['utype']      = $utype;
            $map['uid']        = is_login($utype);
            $map['type']       = ['in', $suffix];
            $map['group_id']   = $groupId;
            $count             = \think\Db::name('attachment')->where($map)->count();
            $list              = \think\Db::name('attachment')->order('id desc')->where($map)->limit($limit)->select();
            $data['totalPage'] = ceil($count / $pageSize);
            $data['list']      = $list;
            return $data;
        }
    }

}